﻿using System.Collections.Generic;
using Entitas;
using System.Linq;
class EnemyAttackTimerSystem : ReactiveSystem<EnemyEntity>, ICleanupSystem
{
    EnemyContext enemyContext;
    public EnemyAttackTimerSystem(Contexts contexts) : base(contexts.enemy)
    {
        enemyContext = contexts.enemy;
    }
    protected override void Execute(List<EnemyEntity> entities)
    {
        foreach (var e in entities)
        {
            e.ReplaceAttackTimer(e.attackTimer.value - .06f);
        }
    }


    protected override bool Filter(EnemyEntity entity)
    {
        return entity.hasAttackTimer;
    }

    protected override ICollector<EnemyEntity> GetTrigger(IContext<EnemyEntity> context)
    {
        return context.CreateCollector(EnemyMatcher.AttackTimer.AddedOrRemoved());
    }

    public void Cleanup()
    {
        foreach (var e in enemyContext.GetEntities().Where(x => x.hasAttackTimer))
        {
            if (e.attackTimer.value <= 0)
            {
                e.RemoveAttackTimer();
                e.isAttacking = false;
            }

        }
    }
}
